
package editor.map
{
	import editor.Notification.M2VNotification;
	import editor.Notification.V2MNotification;
	import editor.common.AppFacade;
	import editor.common.Common;
	import editor.common.UIManager;
	import editor.map.elements.Npc;
	import editor.map.elements.Teleport;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	
	
	/**
	 * @author 缺硒
	 */
	public class NpcLayer extends Sprite
	{
		private var _npcList:Vector.<Npc>;
		private var _teleportList:Vector.<Teleport>;
		
		private var _inGate:Teleport;
		private var _outGate:Teleport;
		
		// 构造函数
		public function NpcLayer()
		{
			_npcList = new Vector.<Npc>();
			_teleportList = new Vector.<Teleport>;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(evt:Event):void
		{
			AppFacade.getIntance().sendNotification(M2VNotification.UPDATE_GATES_INFO,
				{
					inX:_inGate != null ? _inGate.x : 0,
					inY:_inGate != null ? _inGate.y : 0,
					outX:_outGate != null ? _outGate.x : 0,
					outY:_outGate != null ? _outGate.y : 0
				});
		}
		
		public function get outGate():Teleport
		{
			return _outGate;
		}
		
		public function get inGate():Teleport
		{
			return _inGate;
		}
		
		public function get npcList():Vector.<Npc>
		{
			return _npcList;
		}
		
		public function get portList():Vector.<Teleport>
		{
			return _teleportList;
		}
		
		/**
		 * 增加npc
		 * @param id 
		 */		
		public function addNpc(id:String):void
		{
			var npc:Npc = new Npc();
			npc.x = mouseX;
			npc.y = mouseY;
			npc.setLogic();
			npc.npcId = id;
			addChild(npc);
			_npcList.push(npc);
			Common.ui.toolWindow.updateNpcList();
		}
		
		public function getNpcByIndex(idx:int):Npc
		{
			return _npcList[idx] as Npc;
		}
		
		/**
		 * 增加传送点
		 * @param id 
		 */		
		public function addTeleport(id:String):void
		{
			var port:Teleport = new Teleport();
			port.x = mouseX;
			port.y = mouseY;
			port.setLogic();
			port.npcId = id;
			addChild(port);
			_npcList.push(port);
			Common.ui.toolWindow.updateNpcList();
		}
		
		/**
		 * 设置出城点 
		 */		
		public function addOutGate():void
		{
			if(_outGate == null)
			{
				_outGate = new Teleport(0x00ff00);
				_outGate.x = mouseX;
				_outGate.y = mouseY;
				_outGate.setLogic();
				_outGate.label = "出城点";
				_outGate.npcId = "出城点";
				addChild(_outGate);
				AppFacade.getIntance().sendNotification(M2VNotification.ADD_OUT_OK);
			}
			//
		}
		
		/**
		 * 设置进城点 
		 */		
		public function addInGate():void
		{
			if(_inGate == null)
			{
				_inGate = new Teleport();
				_inGate.x = mouseX;
				_inGate.y = mouseY;
				_inGate.setLogic();
				_inGate.label = "进城点";
				_inGate.npcId = "进城点";
				addChild(_inGate);
				AppFacade.getIntance().sendNotification(M2VNotification.ADD_IN_OK);
			}
		}
		//
	}
}